#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置背景图片
    setBackgroundImage(":/images/images/game.jpg");
        QPixmap background(":/images/images/game.jpg"); // 替换为你的图片路径
        if (!background.isNull()) {
            // 缩放背景图以适应窗口大小
            background = background.scaled(this->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

            // 设置背景
            QPalette palette;
            palette.setBrush(QPalette::Window, QBrush(background));
            this->setPalette(palette);
        }

    choose_hero chooseWindow;
    game_view gameWindow;
    // 创建堆叠窗口用于管理多个界面

    stackedWidget = new QStackedWidget(this);
    //堆叠窗口透明
     stackedWidget->setStyleSheet("background-color: transparent;");

    // 创建三个独立界面
    startindex = new start_index();
    chooselevel = new choose_level();
    gameview = new game_view();
    choosehero = new choose_hero();
    Rest = new class rest();

    // 设置所有子界面为透明
    startindex->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    chooselevel->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    gameview->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    choosehero->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    Rest->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    // 将界面添加到堆叠窗口
    stackedWidget->addWidget(startindex);
    stackedWidget->addWidget(chooselevel);
    stackedWidget->addWidget(gameview);
    stackedWidget->addWidget(choosehero);
    stackedWidget->addWidget(Rest);
    // 设置主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(stackedWidget);
    setLayout(mainLayout);

    // 连接信号和槽，实现界面切换和窗口关闭
    connect(startindex, SIGNAL(goToChooseHero()), this, SLOT(goToChooseHero()));


    connect(chooselevel, SIGNAL(goToGameView()), this, SLOT(showPage3()));
    connect(chooselevel, SIGNAL(goTostartgame()), this, SLOT(goTostartgame()));

    connect(choosehero,SIGNAL(showPage2()),this,SLOT(showPage2()));

    connect(gameview,SIGNAL(rest()),this,SLOT(rest()));

    connect(Rest,SIGNAL(showPage3()),this,SLOT(showPage3()));
    connect(Rest,SIGNAL(showPage2()),this,SLOT(showPage2()));
    // 默认显示第一个界面
    stackedWidget->setCurrentWidget(startindex);
}

void Widget::setBackgroundImage(const QString &imagePath)
{
    QPixmap background(imagePath);
    if (!background.isNull()) {
        QPalette palette;
        palette.setBrush(QPalette::Window,
        QBrush(background.scaled(this->size(),
        Qt::IgnoreAspectRatio,
        Qt::SmoothTransformation)));
        this->setPalette(palette);
    }
}

// 窗口大小改变时重新调整背景图大小
void Widget::resizeEvent(QResizeEvent *event)
{
    // 先调用父类的resizeEvent确保正常处理
        QWidget::resizeEvent(event);
        // 更新背景图
        setBackgroundImage(":/images/images/game.jpg");
}

Widget::~Widget()
{
    delete stackedWidget->widget(0);
    delete stackedWidget->widget(1);
    delete stackedWidget->widget(2);
    delete stackedWidget->widget(3);
    delete stackedWidget->widget(4);
    delete stackedWidget;
    delete ui;
}

void Widget::showPage2()
{
    stackedWidget->setCurrentWidget(chooselevel);
}

void Widget::showPage3()
{
    stackedWidget->setCurrentWidget(gameview);
    gameview->updateHeroValueLabel("法外狂徒");
}

void Widget::goToChooseHero()
{
    stackedWidget->setCurrentWidget(choosehero);
}

void Widget::goTostartgame()
{
    stackedWidget->setCurrentWidget(startindex);
}

void Widget::rest()
{
    stackedWidget->setCurrentWidget(Rest);
    Rest->select_gear_name("法外狂徒");
}




